/**
 * 数据字典子项面板
 */
Ext.namespace("Ext.ux.syscode");

Ext.ux.syscode.ItemPanel = Ext.extend(Ext.Panel, {

   	region: 'center',
	layout: 'border',
	border: false,
	
	typeCode: '',
	
	formPanel: null,
    grid: null,
    
    
	// 组件事件初始化
	initComponent: function(){
		Ext.ux.syscode.ItemPanel.superclass.initComponent.call(this);

		// 定义条件查询面板
		var formPanel = new Ext.FormPanel({
			region: 'north',
			title: '数据字典子项查询',
			frame: true,
			height: 65,
			labelWidth: 70,	// label的长度
			labelAlign: 'right',
			items: [{
				layout: 'column',
				items:[{
					columnWidth: .3,
					layout: 'form',
					items: [{
						xtype: 'textfield',
						fieldLabel: '子项编号',
						name: 'itemCode',
						anchor: '95%'
					}]
				},{
					columnWidth: .3,
					layout: 'form',
					items: [{
						xtype: 'textfield',
						fieldLabel: '子项名称',
						name: 'itemName',
						anchor: '95%'
					}]
				},{
					columnWidth: .4,
					layout: 'hbox',
					defaults:{margins:'0 5 0 0'},
					items: [{
						xtype: 'button',
						text: '查询',
						handler: this.search,
						scope: this
					},{
						xtype: 'button',
						text: '清空',
						handler: this.clearSearchForm,
						scope: this
					}]
				}]
			}]
	    });
		this.add(formPanel);
	    this.formPanel = formPanel;
		
	    
		// 定义业务字典查询显示列模型
		var gridStructure = [{ 
			name: 'itemId',
			hidden: true
		},{
			name: 'typeCode',
			hidden: true
		},{
			name: 'itemCode',  
			header: "子项编号",
			sortable: true,
			editor: new Ext.form.TextField({
					allowBlank: false,
					maxLength: 20
				}),
			width: 25
		},{
			name: 'itemName',  
			header: "子项名称",
			sortable: true,
			editor: new Ext.form.TextField({
					allowBlank: false,
					maxLength: 200
				}),
			width: 25
		},{
			type: 'boolean',
			name: 'itemStatus',  
			header: "是否启用",
			sortable: true,
			editor: new Ext.ux.TwoItemComboBox({
	            itemType: 'boolean',
	            allowBlank: false
			}),
			width: 10
		},{
			name: 'itemSortno',
			header: "排序",
			sortable: true,
			editor: new Ext.form.TextField({
					allowBlank: false,
					maxLength: 20
			}),
			width: 10
		},{
			name: 'itemMemo',
			header: "备注",
			sortable: true,
			editor: new Ext.form.TextField({
					maxLength: 100,
					allowBlank: true
				}),
			width: 15
		}];
		
		// 定义业务字典项表格对象	
		var itemGrid = new Ext.ux.grid.RowEditorGrid({
	 		title: '数据字典子项列表',
	 		region: 'center',
	 		margins: '2 0 0 0',
	 		preload: false,
	 		structure: gridStructure,
			defaultSortField: 'itemSortno',  
			idField: 'itemId',
			readUrl: 'sys-code!itemGridList.action',
			createUrl: 'sys-code!itemGridCreate.action',
			updateUrl: 'sys-code!itemGridUpdate.action',
			destroyUrl: 'sys-code!itemGridDelete.action'
	 	});
	 	
	    this.add(itemGrid);
	    this.grid = itemGrid;
	    		
	 	// 在执行查询操作前传入查询条件参数
		itemGrid.store.on('beforeload', function() {
			itemGrid.store.baseParams = {
				typeCode: this.selTypeCode,
				itemCode: this.formPanel.form.findField('itemCode').getValue(),
				itemName: this.formPanel.form.findField('itemName').getValue()
			};
		}, this);
	},
	
	// 条件查询
	search: function(){
		this.grid.store.reload();
	},
	
	// 清空
	clearSearchForm: function(){
		this.formPanel.form.reset();
		this.formPanel.form.findField('itemCode').focus();
		this.grid.store.reload();
	},
	
	//根据类型编号重新加载数据
	reload: function(typeCode){
		this.selTypeCode = typeCode;
		this.setRecordTypeOptions();
		this.grid.store.reload();
	},
	
	//设置itemgrid中特定的记录类型
	setRecordTypeOptions: function(){
        this.grid.recordTypeOptions = {
	        itemId: '',
	        typeCode: this.selTypeCode,
			itemCode: '',
			itemName: '',
			itemStatus: '1',
			itemSortno: '0',
			itemMemo: ''
        };
	},
	
	//清空数据
	clearData: function(){
		this.reload('');
		this.setRecordTypeOptions();
	}
});
	
